/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Robert
 */
public class Configuration {

    private Properties defaultProps;
    private Properties appProps;
    private static Configuration instance;
    private static final String SIMULATION_SPEED = "simulation_speed";

    public Configuration() {
        InputStream in = null;
        try {
            defaultProps = new Properties();
            in = this.getClass().getResourceAsStream("defaultProperties.properties");
            defaultProps.load(in);
            in.close();
            appProps = new Properties(defaultProps);

            // now load properties from last invocation
            in = this.getClass().getResourceAsStream("appProperties.properties");
            appProps.load(in);
        } catch (IOException ex) {
            Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public static Configuration getInstance() {
        if (instance == null) {
            instance = new Configuration();
        }
        return instance;
    }

    public long getSimulationSpeed() {
        return Integer.parseInt(appProps.getProperty(SIMULATION_SPEED));
    }

    public void setSimulationSpeed(long simulationSpeed) {
        appProps.setProperty(SIMULATION_SPEED, Long.toString(simulationSpeed));
        save();
    }

    

    protected void save() {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(this.getClass().getResource("appProperties.properties").getPath());
            appProps.store(out, "---No Comment---");
        } catch (IOException ex) {
            Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
     
    }
}
